﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using KaoQin.BLL;
using KaoQin.Model;
using KaoQin.Common;
using System.Text.RegularExpressions;

namespace KaoQin.WPFApp
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {
        public LoginWindow()
        {
            InitializeComponent();
        }
        private bool ispass = false;
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                
                if (Validation.GetHasError(txtUserName))
                {
                    return;
                }
                
                
                if (string.IsNullOrEmpty(txtUserName.Text.Trim()))
                {
                    MessageBox.Show("用户名不能为空");
                    return;
                }
                if (string.IsNullOrEmpty(txtUserPwd.Password.Trim()))
                {
                    MessageBox.Show("密码不能为空");
                    return;
                }

                if (!ispass)
                {
                    return;
                }
                SysUsersInfo model= new SysUsers().GetModelByName(txtUserName.Text);
                if (model == null)
                {
                    MessageBox.Show("用户名错误");
                    return;
                }
                if (model.Password != txtUserPwd.Password)
                {
                    MessageBox.Show("密码错误");
                    return;
                }
                
                MainWindow main = new MainWindow();
                main.LoginUser = model;
                main.Show();
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("登录失败:" + ex.Message);
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.gridLogin.DataContext = new SysUsersInfo();
            
        }

        private void txtUserPwd_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (!Regex.IsMatch(txtUserPwd.Password.Trim(), "^[0-9a-zA-Z_]{1,20}$"))
            {
                this.txtUserPwd.Style = Resources["noRule"] as Style;
                txtUserPwd.ToolTip = "只能输入二十位以下的数字、字母或下划线";
                ispass = false;
            }
            else
            {
                this.txtUserPwd.Style = null;
                ispass = true;
            }
        }

       
    }
}
